

import tweepy

class MyStreamListener(tweepy.StreamListener): 

 def on_connect(self): 
  print("[on_connect]")
 

 def on_data(self, raw_data): 
  print("[on_data]",raw_data)
  with open('.txt','a') as tb:
   tb.write(raw_data)
 
 
 def on_status(self, status): 
  print("[on_status]",status.text) 
 
 
 def keep_alive(self): 
  print("[keep_alive]") 
 
 
 def on_exception(self, exception): 
  print("[on_exception] Called when an unhandled exception occurs.", exception)  
 
 def on_delete(self, status_id, user_id): 
  print("[on_delete] Called when a delete notice arrives for a status status_id=%d, user_id=%d" % status_id, user_id) 
 
 
 def on_event(self, status): 
  print("[on_event] Called when a new event arrives", status) 
 
 
 def on_direct_message(self, status): 
  print("[on_direct_message] Called when a new direct message arrives", status) 
 
 
 def on_friends(self, friends): 
  print("[on_friends] Called when a friends list arrives.", friends) 
 
 def on_limit(self, track): 
  print("[on_limit] Called when a limitation notice arrives", track) 
 
 
 def on_timeout(self): 
  print("[on_timeout] Called when stream connection times out") 
 
 
 def on_disconnect(self, notice): 
  print("[on_disconnect] Called when twitter sends a disconnect notice", notice) 
 
 
 def on_warning(self, notice): 
  print("[on_warning] Called when a disconnection warning message arrives", notice) 
 
 
 def on_error(self, status_code): 
  print("[on_error] Called when a non-200 status code is returned", status_code) 
 		
  if status_code == 420: 
    #returning False in on_data disconnects the stream 
    print("\n[on_error] !!!Limite atingido!!!\n") 
    return False 
 
 
API_key="" 
API_secret_key="" 
access_token="" 
access_token_secret="" 

auth = tweepy.OAuthHandler(API_key, API_secret_key) 
auth.set_access_token(access_token, access_token_secret) 
api = tweepy.API(auth) 
 
while True:
  try:
 
    myStreamListener = MyStreamListener() 
    myStream = tweepy.Stream(auth = api.auth, listener=MyStreamListener()) 
    myStream.filter(track=["", "", ""])
  except:
    continue
